package com.devilloki.viki.overspeedalert.fragments;

import android.app.Fragment;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.devilloki.viki.overspeedalert.Events.GpsInfoEvent;
import com.devilloki.viki.overspeedalert.R;
import com.devilloki.viki.overspeedalert.tools.DataConvert;
import com.devilloki.viki.overspeedalert.tools.MY_NAMES;
import com.devilloki.viki.overspeedalert.tools.SettingsHelper;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**显示gps信息的fragment
 * Created by viki on 2016/3/24.
 */
public class DisplayGpsInfoFragment extends Fragment {
    /**
     * 显示经纬度的TextView
     */
    private TextView tvGPSInfo=null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.display_gpsinfo, container, false);

        tvGPSInfo= (TextView) view.findViewById(R.id.tv_gpsinfo);
        if (savedInstanceState!=null){
            tvGPSInfo.setText(
                    savedInstanceState.getString(
                            MY_NAMES.KEY_GPS_INFO,MY_NAMES.DATA_Default_LngAndLat));
        }

        return view;
    }
    /**
     * 根据位置位置的变化更新界面
     */
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onEventDisplayGpsInfo(GpsInfoEvent event) {
        double lng=event.getLongitude();
        double lat= event.getLatitude();
        double alt=event.getAltitude();

        //生成经纬度海拔信息的字符串
        String tempStr=DataConvert.formatData(lat,6) + this.getString(R.string.falg_lat)+MY_NAMES.STR_fen_ge_fu
                + DataConvert.formatData(lng,6) + this.getString(R.string.falg_lng)+MY_NAMES.STR_fen_ge_fu
                + DataConvert.formatData(alt,1) + this.getString(R.string.distanceM);
        tvGPSInfo.setText(tempStr);

    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(MY_NAMES.KEY_GPS_INFO, (String) tvGPSInfo.getText());
    }
}
